package com.zjd.association.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjd.association.common.PageWrapper;
import com.zjd.association.entity.ApplyLogs;
import com.zjd.association.entity.Members;
import com.zjd.association.entity.Teams;
import com.zjd.association.mapper.ApplyLogsMapper;
import com.zjd.association.mapper.MembersMapper;
import com.zjd.association.mapper.TeamsMapper;
import com.zjd.association.service.ApplyLogsService;
import com.zjd.association.utils.DateUtils;
import com.zjd.association.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 申请记录 服务层实现类
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@Service
@RequiredArgsConstructor
public class ApplyLogsServiceImpl implements ApplyLogsService {

    private final MembersMapper membersMapper;

    private final ApplyLogsMapper applyLogsMapper;

    private final TeamsMapper teamsMapper;

    @Override
    public void add(ApplyLogs applyLogs) {
        applyLogsMapper.insert(applyLogs);
    }

    @Override
    public void update(ApplyLogs applyLogs) {
        if (applyLogs.getStatus() != null && applyLogs.getStatus() == 1) {
            Members member = new Members();
            member.setId(UUIDUtils.makeIDByCurrent());
            member.setCreateTime(DateUtils.getNowDate());
            member.setUserId(applyLogs.getUserId());
            member.setTeamId(applyLogs.getTeamId());

            membersMapper.insert(member);
            Teams teams = teamsMapper.selectById(applyLogs.getTeamId());
            teams.setTotal(teams.getTotal() + 1);

            teamsMapper.updateById(teams);
        }

        applyLogsMapper.updateById(applyLogs);
    }

    @Override
    public void delete(ApplyLogs applyLogs) {
        applyLogsMapper.deleteById(applyLogs);
    }

    /**
     * 检查用户是否可以提交申请
     *
     * @param userId 用户ID
     * @param teamId 团队ID
     * @return 结果
     */
    @Override
    public Boolean isApply(String userId, String teamId) {
        QueryWrapper<ApplyLogs> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("team_id", teamId);
        qw.eq("status", 0);
        return applyLogsMapper.selectCount(qw) <= 0;
    }

    @Override
    public ApplyLogs getOne(String id) {
        ApplyLogs applyLogs = applyLogsMapper.selectById(id);
        return applyLogs;
    }

    /**
     * 团队管理员分页查询申请记录信息
     *
     * @param pageIndex 当前页码
     * @param pageSize  每页数据量
     * @param userId    用户编号
     * @param teamName  团队名称
     * @param userName  用户姓名
     * @return 申请记录
     */
    @Override
    public PageWrapper getManPageInfo(Long pageIndex, Long pageSize, String userId, String teamName, String userName) {
        Page<Map<String, Object>> page
                = applyLogsMapper.qryManPageInfo(new Page<>(pageIndex, pageSize), userId, teamName, userName);
        return PageWrapper.parsePage(page);
    }

    /**
     * 分页查询申请记录信息
     *
     * @param pageIndex 当前页码
     * @param pageSize  每页数据量
     * @param userId    用户编号
     * @param teamName  团队名称
     * @param userName  用户姓名
     * @return 申请记录
     */
    @Override
    public PageWrapper getPageInfo(Long pageIndex, Long pageSize, String userId, String teamName, String userName) {
        Page<Map<String, Object>> page =
                applyLogsMapper.qryPageInfo(new Page<>(pageIndex, pageSize), userId, teamName, userName);
        return PageWrapper.parsePage(page);
    }

}